package com.hs.telepass.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.hs.telepass.model.RepositoryProfile;
import com.hs.telepass.model.TelepassProfiles;
import com.hs.telepass.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class RepositoryProfilesDatabase {
    private Context mContext;

    public static final String TABLE_NAME = "profilesRepository";
    public static final String COLUMN_KEY_ID = "id";
    public static final String COLUMN_SERVICE_ID = "serviceId";
    public static final String COLUMN_SERVICE_NAME = "serviceName";
    public static final String COLUMN_VERSION = "version";
    public static final String COLUMN_TITLE = "title";
    public static final String COLUMN_INFOURL = "infoURL";
    public static final String COLUMN_LOGOURL = "logoURL";
    public static final String COLUMN_KEYURL = "keyURL";

    public RepositoryProfilesDatabase(Context mContext) {
        this.mContext = mContext;
    }

    public long addRecord(RepositoryProfile profile) {
        List<RepositoryProfile> recordtList = new ArrayList<RepositoryProfile>();
        recordtList.add(profile);
        return addRecords(recordtList).get(0);
    }

    public List<Long> addRecords(List<RepositoryProfile> recordList) {
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        List<Long> idList = new ArrayList<Long>();
        for (RepositoryProfile profile : recordList) {
            ContentValues cv = new ContentValues();
            cv.put(COLUMN_SERVICE_ID, profile.getServiceId());
            cv.put(COLUMN_SERVICE_NAME, profile.getServiceName());
            cv.put(COLUMN_TITLE, profile.getTitle());
            cv.put(COLUMN_VERSION, profile.getVersion());
            cv.put(COLUMN_INFOURL, profile.getInfoURL());
            cv.put(COLUMN_LOGOURL, profile.getLogoURL());
            cv.put(COLUMN_KEYURL, profile.getKeyURL());

            long id = db.insert(TABLE_NAME, null, cv);
            idList.add(id);
        }

        return idList;
    }

    public List<RepositoryProfile> getProfiles() {
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        List<RepositoryProfile> list = new ArrayList<RepositoryProfile>();
        String sql = "select * from " + TABLE_NAME;
        Cursor cursor = db.rawQuery(sql, null);
        while(cursor.moveToNext()) {
            RepositoryProfile profile = new RepositoryProfile();
            int id = cursor.getInt(cursor.getColumnIndex(COLUMN_KEY_ID));
            String title = cursor.getString(cursor.getColumnIndex(COLUMN_TITLE));
            String serviceId = cursor.getString(cursor.getColumnIndex(COLUMN_SERVICE_ID));
            String serviceName = cursor.getString(cursor.getColumnIndex(COLUMN_SERVICE_NAME));
            String version = cursor.getString(cursor.getColumnIndex(COLUMN_VERSION));
            String infoUrl = cursor.getString(cursor.getColumnIndex(COLUMN_INFOURL));
            String logoURL = cursor.getString(cursor.getColumnIndex(COLUMN_INFOURL));
            String keyUrl = cursor.getString(cursor.getColumnIndex(COLUMN_KEYURL));

            profile.setId(id);
            profile.setTitle(title);
            profile.setServiceId(serviceId);
            profile.setServiceName(serviceName);
            profile.setVersion(version);
            profile.setInfoURL(infoUrl);
            profile.setLogoURL(logoURL);
            profile.setKeyURL(keyUrl);

            list.add(profile);
        }
        cursor.close();
        return list;
    }

    /**
     * 更新数据状态
     * */
    public void updateProfile(int id, String username, String password) {
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);

    }

    public int deleteProfile(int id){
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        String where = COLUMN_KEY_ID + "=?";
        String[] whereArgs = {String.valueOf(id)};
        return db.delete(TABLE_NAME, where, whereArgs);
    }

/*


    public void clearRecords(){
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        DatabaseHelper dh = DatabaseHelper.getHelper(mContext);
        dh.clearLoginTable(db);
    }

    public int deleteRecord(long id){
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        String where = COLUMN_KEY_ID + "=?";
        String[] whereArgs = {String.valueOf(id)};
        return db.delete(TABLE_NAME, where, whereArgs);
    }*/


}
